package com.LiL.Wayne.Activity.activity;


import java.io.FileInputStream;






import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.R.id;
import com.LiL.Wayne.Activity.R.layout;
import com.audamob.audasingers.model.Lyric;
import com.audamob.audasingers.model.Music;
import com.audamob.audasingers.model.PlayList;
import com.audamob.audasingers.model.Song;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.SpeechRecognizer;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.InputFilter.LengthFilter;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView.OnEditorActionListener;
import android.webkit.WebView;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;



import android.util.*;
public class SelectPlaylist extends Activity{
	private SimpleDateFormat FormatterDuration = new SimpleDateFormat("mm:ss");
	ArrayList<PlayList> PlaylistMusicList;
	EditText NewPlaylistName;
	
	
      
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        	// TODO Auto-generated method stub
        	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        	  setContentView(R.layout.select_playlist);
    		  Bundle b=getIntent().getExtras();
            PlaylistMusicList=new ArrayList<PlayList>();
            try {
				PlaylistMusicList=restorePlayListList();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	NewPlaylistName=(EditText)findViewById(R.id.newPlaylist);
         	RelativeLayout Done=(RelativeLayout)findViewById(R.id.Done);
         	Done.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					InputMethodManager imm = (InputMethodManager)getSystemService(
						      Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(NewPlaylistName.getWindowToken(), 0);
					 String playlistname=NewPlaylistName.getText().toString();
					 if(playlistname.length()>0){
						 
						 boolean existe=false;
						 for(int i=0;i<PlaylistMusicList.size()&&!existe;i++){
							 if(PlaylistMusicList.get(i).getName().equalsIgnoreCase(playlistname)){
								 existe=true;
								 break;
							 }
						 }
						 if(!existe){
							 PlaylistMusicList.add(new PlayList(playlistname, new ArrayList<Music>()));
						 }
						
						 dataPlaylistName=playlistname;
						 
					 }else{
						 boolean existe=false;
						 for(int i=0;i<PlaylistMusicList.size()&&!existe;i++){
							 if(PlaylistMusicList.get(i).getName().equalsIgnoreCase("Default PlayList")){
								 existe=true;
								 break;
							 }
							
						 }
						 if(!existe){
							 PlaylistMusicList.add(new PlayList("Default PlayList", new ArrayList<Music>()));
						 }
						 dataPlaylistName="Default PlayList";
						 
					 }
					 try {
						sauvegardPlayListList(PlaylistMusicList);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					  data.setType(dataPlaylistName);
			    	  setResult(3,data);
					finish();
				}
			});
         	super.onCreate(savedInstanceState);
        }
        
            
        
     
   
	 
  	
    	
	 public ArrayList<PlayList> restorePlayListList() throws IOException, ClassNotFoundException {
			FileInputStream fin = new FileInputStream(getCacheDir().getAbsolutePath()+"/playlistlist");
			ObjectInputStream in = new ObjectInputStream(fin);
			ArrayList<PlayList> object = (ArrayList<PlayList>)in.readObject();
			in.close();
			return object;
		}
	 public void sauvegardPlayListList(ArrayList<PlayList> s) throws IOException {
			FileOutputStream fout = new FileOutputStream(getCacheDir().getAbsolutePath()+"/playlistlist");
			ObjectOutputStream out = new ObjectOutputStream(fout);
			
			out.writeObject(s);
			out.close();
		}
	 Intent data=new Intent();
	 String dataPlaylistName="";
	 @Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		 String playlistname=NewPlaylistName.getText().toString();
		 if(playlistname.length()>0){
			 
			 boolean existe=false;
			 for(int i=0;i<PlaylistMusicList.size()&&!existe;i++){
				 if(PlaylistMusicList.get(i).getName().equalsIgnoreCase(playlistname)){
					 existe=true;
					 break;
				 }
			 }
			 if(!existe){
				 PlaylistMusicList.add(new PlayList(playlistname, new ArrayList<Music>()));
			 }
			
			 dataPlaylistName=playlistname;
			 
		 }else{
			 boolean existe=false;
			 for(int i=0;i<PlaylistMusicList.size()&&!existe;i++){
				 if(PlaylistMusicList.get(i).getName().equalsIgnoreCase("Default PlayList")){
					 existe=true;
					 break;
				 }
				
			 }
			 if(!existe){
				 PlaylistMusicList.add(new PlayList("Default PlayList", new ArrayList<Music>()));
			 }
			 dataPlaylistName="Default PlayList";
			 
		 }
		 try {
			sauvegardPlayListList(PlaylistMusicList);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  data.setType(dataPlaylistName);
    	  setResult(3,data);
		super.onBackPressed();
	}
      @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	  data.setType(dataPlaylistName);
    	  setResult(3,data);
    	super.onDestroy();
    } 	 
    	 
}